package comp6231.project.common;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.math.BigInteger;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import javax.swing.SpringLayout;

public class UDPTester extends JFrame {

	private JPanel contentPane;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_2;
	private JTextField txtLocalhost;
	private JTextField textField_2;
	private JLabel lblNewLabel_3;
	private JLabel lblNewLabel_4;
	private JLabel lblNewLabel_5;
	private JTextField textField_3;
	private JTextField txtBuy;
	private JTextField textField_5;
	private JLabel lblNewLabel_6;
	private JLabel lblNewLabel_7;
	private JTextField textField_6;
	private JTextField textField_7;
	private JLabel lblNewLabel_8;
	private JLabel lblNewLabel_9;
	private JTextField textField_8;
	private JTextField textField_9;
	private JButton btnNewButton;
	private JTextField textField;
	private JLabel lblHost;
	private JTextField textField_1;
	private JLabel lblResult;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UDPTester frame = new UDPTester();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public UDPTester() {
		setTitle("UDPTester");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 526, 503);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);
		
		lblNewLabel = new JLabel("Target");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel, 23, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel, 155, SpringLayout.WEST, contentPane);
		contentPane.add(lblNewLabel);
		
		txtLocalhost = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, txtLocalhost, 20, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, txtLocalhost, 193, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, txtLocalhost, 503, SpringLayout.WEST, contentPane);
		txtLocalhost.setText("localhost");
		contentPane.add(txtLocalhost);
		txtLocalhost.setColumns(10);
		
		lblNewLabel_2 = new JLabel("Port");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel_2, 56, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel_2, 170, SpringLayout.WEST, contentPane);
		contentPane.add(lblNewLabel_2);
		
		textField_2 = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textField_2, 53, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, textField_2, 193, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, textField_2, 503, SpringLayout.WEST, contentPane);
		textField_2.setText("10100");
		contentPane.add(textField_2);
		textField_2.setColumns(10);
		
		lblNewLabel_3 = new JLabel("sequenceNum");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel_3, 116, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel_3, 113, SpringLayout.WEST, contentPane);
		contentPane.add(lblNewLabel_3);
		
		textField_3 = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textField_3, 113, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, textField_3, 193, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, textField_3, 503, SpringLayout.WEST, contentPane);
		textField_3.setText("1");
		contentPane.add(textField_3);
		textField_3.setColumns(10);
		
		lblNewLabel_4 = new JLabel("request");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel_4, 145, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel_4, 150, SpringLayout.WEST, contentPane);
		contentPane.add(lblNewLabel_4);
		
		txtBuy = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, txtBuy, 142, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, txtBuy, 193, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, txtBuy, 503, SpringLayout.WEST, contentPane);
		txtBuy.setText("BUY");
		contentPane.add(txtBuy);
		txtBuy.setColumns(10);
		
		lblNewLabel_5 = new JLabel("customerID");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel_5, 174, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel_5, 128, SpringLayout.WEST, contentPane);
		contentPane.add(lblNewLabel_5);
		
		textField_5 = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textField_5, 171, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, textField_5, 193, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, textField_5, 503, SpringLayout.WEST, contentPane);
		textField_5.setText("00001");
		contentPane.add(textField_5);
		textField_5.setColumns(10);
		
		lblNewLabel_6 = new JLabel("itemID");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel_6, 203, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel_6, 156, SpringLayout.WEST, contentPane);
		contentPane.add(lblNewLabel_6);
		
		textField_6 = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textField_6, 200, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, textField_6, 193, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, textField_6, 503, SpringLayout.WEST, contentPane);
		textField_6.setText("0001");
		contentPane.add(textField_6);
		textField_6.setColumns(10);
		
		lblNewLabel_7 = new JLabel("numItems");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel_7, 232, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel_7, 136, SpringLayout.WEST, contentPane);
		contentPane.add(lblNewLabel_7);
		
		textField_7 = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textField_7, 229, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, textField_7, 193, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, textField_7, 503, SpringLayout.WEST, contentPane);
		textField_7.setText("5");
		contentPane.add(textField_7);
		textField_7.setColumns(10);
		
		lblNewLabel_8 = new JLabel("exchangeItemID");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel_8, 261, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel_8, 101, SpringLayout.WEST, contentPane);
		contentPane.add(lblNewLabel_8);
		
		textField_8 = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textField_8, 258, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, textField_8, 193, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, textField_8, 503, SpringLayout.WEST, contentPane);
		contentPane.add(textField_8);
		textField_8.setColumns(10);
		
		lblNewLabel_9 = new JLabel("numExchangeItems");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel_9, 290, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel_9, 82, SpringLayout.WEST, contentPane);
		contentPane.add(lblNewLabel_9);
		
		textField_9 = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textField_9, 287, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, textField_9, 193, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, textField_9, 503, SpringLayout.WEST, contentPane);
		textField_9.setText("0");
		contentPane.add(textField_9);
		textField_9.setColumns(10);
		
		btnNewButton = new JButton("Send");
		sl_contentPane.putConstraint(SpringLayout.WEST, btnNewButton, 193, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, btnNewButton, -10, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, btnNewButton, 503, SpringLayout.WEST, contentPane);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					String target = txtLocalhost.getText();
					int port = Integer.parseInt(textField_2.getText());
					send(getMessage(), target, port);
				} catch (NumberFormatException nfe) {
					nfe.printStackTrace();
				}
			}
		});
		contentPane.add(btnNewButton);
		
		textField = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textField, 32, SpringLayout.SOUTH, textField_9);
		textField.setColumns(10);
		contentPane.add(textField);
		
		lblHost = new JLabel("replicaID");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblHost, 3, SpringLayout.NORTH, textField);
		sl_contentPane.putConstraint(SpringLayout.EAST, lblHost, 0, SpringLayout.EAST, lblNewLabel);
		contentPane.add(lblHost);
		
		textField_1 = new JTextField();
		textField_1.setText("0");
		sl_contentPane.putConstraint(SpringLayout.SOUTH, textField_1, -18, SpringLayout.NORTH, btnNewButton);
		sl_contentPane.putConstraint(SpringLayout.EAST, textField, 0, SpringLayout.EAST, textField_1);
		sl_contentPane.putConstraint(SpringLayout.WEST, textField_1, 0, SpringLayout.WEST, txtLocalhost);
		textField_1.setColumns(10);
		contentPane.add(textField_1);
		
		lblResult = new JLabel("result");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblResult, 3, SpringLayout.NORTH, textField_1);
		sl_contentPane.putConstraint(SpringLayout.EAST, lblResult, 0, SpringLayout.EAST, lblNewLabel);
		contentPane.add(lblResult);
	}
	
	private Message getMessage() {
		Message msg = new Message();
		try {
			msg.sequenceNum = BigInteger.valueOf(Integer.parseInt(textField_3.getText()));
			msg.request = txtBuy.getText();
			msg.customerID = textField_5.getText();
			msg.itemID = textField_6.getText();
			msg.numItems = Integer.parseInt(textField_7.getText());
			msg.exchangeItemID = textField_8.getText();
			msg.numExchangeItems = Integer.parseInt(textField_9.getText());
			msg.replicaID = textField.getText();
			msg.result = Integer.parseInt(textField_1.getText());
		} catch (NumberFormatException nfe) {
			nfe.printStackTrace();
		}		
		return msg;
	}
	
	private static void send(Message msg, String host, int port) {
		DatagramSocket ds = null;
		try {
			InetAddress dest = InetAddress.getByName(host);
			byte bufOut[] = Utils.toBytes(msg);
			DatagramPacket pack = new DatagramPacket(bufOut, bufOut.length,
					dest, port);
			ds = new DatagramSocket();
			ds.send(pack);
			ds.close();

		} catch (Exception ex) {
			ex.printStackTrace();

		} finally {
			if (ds != null) {
				ds.close();
			}
		}		
	}
}
